<?php
/*
 * allDepos.php
 * 
 * Copyright 2013  <>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
	<title>All Deposits</title>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
	<meta name="generator" content="Geany 1.22" />
</head>

<body>

<!--
    html form: "sort data" options and a couple of filters. The sorting is left to the mysql engine,
    the results filtering is doing by php (I want to practise php array handling)
-->
	<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
		<fieldset style="width:40%">
			<legend><b>Sort By</b></legend>
			<b>1:</b> <select name="order_by[0]" size="1">
				<option></option>
				<option>material</option>
				<option>size</option>
				<option>time_mod</option>
			</select>
			<input type="radio" name="descending[0]" value="<?php echo FALSE;?>" checked> Ascending
			<input type="radio" name="descending[0]" value="<?php echo TRUE;?>" > Descending
			<br><br>
			<b>2:</b> <select name="order_by[1]" size="1">
				<option></option>
				<option>material</option>
				<option>size</option>
				<option>time_mod</option>
			</select>
			<input type="radio" name="descending[1]" value="<?php echo FALSE;?>" checked> Ascending
			<input type="radio" name="descending[1]" value="<?php echo TRUE;?>" > Descending
			<br><br>
			<b>3:</b> <select name="order_by[2]" size="1">
				<option></option>
				<option>material</option>
				<option>size</option>
				<option>time_mod</option>
			</select>
			<input type="radio" name="descending[2]" value="<?php echo FALSE;?>" checked> Ascending
			<input type="radio" name="descending[2]" value="<?php echo TRUE;?>" > Descending
		</fieldset>

	
	<?php
		
		// include of required files
		require_once ("constants.php");
		require_once ("planets_class.php");
		require_once ("deposits_class.php");

			
	?>
<!--
	    filter by fieldset: you can filter by planet and/or deposit type
-->
		<fieldset style="width:40%">
			<legend><b>Filter By</b></legend>
			<div>
			Planet: <select name="planet_name_filter" size="1">
				<option></option>
			<?php
				$PlanetData = planets::ListAllPlanets();

				//build the list with all the planets in the db
				foreach ($PlanetData as $planet){
					echo "<option>".$planet['name']."</option>\n";
				}

			?>
			</div>
			<div>
			</select>
			&nbsp;Material: <select name="material_filter" size="1">
				<option></option>
			<?php
				foreach($rm_array as $mat){
					echo "<option>".$mat."</option>\n";
				}
			?>
			</select>
			</div>
		</fieldset>
		<p><input type="submit" value="Submit" name="enviar"></p>
	</form>
	<?php

			//Select deposits preparation. If there is sorting data posted, set the $orderby parameter
			//of the SelectAllDeposits function
			if(!empty($_POST['order_by'][0])){
				
				foreach($_POST['order_by'] as $key => $value){
					if($value) {
						$orderby[] = array((string) $value, (boolean) $_POST['descending'][$key]);
					}
				}

			} else {
				$orderby = FALSE;
			}
			
			//array with all the deposits
			$data = deposits::SelectAllDeposits($orderby);
			

			// if data, display a table of deposits
			if($data){
	
				//html table
				echo "<table width=\"75%\" border=1 RULES=ALL FRAME=BOX align=\"center\" cellspacing=\"0\" cellpadding=\"0\">\n";
				echo "<tr align=\"center\" valign=\"center\"><th>Planet</th><th>Position</th><th>Material</th><th>Size</th><th>User</th><th>Last Modified</th></tr>\n";
				
				//insert a row in the html table for each element in the $data array
				//if filtering rules are set, filters the results
				foreach($data as $depos){
					
					if(!empty($_POST['planet_name_filter'])){
						if($_POST['planet_name_filter'] !== $depos['name']){
							continue;
						}
					}
					if(!empty($_POST['material_filter'])){
						if($_POST['material_filter'] !== $depos['material']){
							continue;
						}
					}

					echo "<tr align=\"center\" valign=\"center\">\n";

					echo "<td>".$depos['name']."</td><td>(".$depos['x_coord'].", ".$depos['y_coord'].")</td><td>".$depos['material']."</td><td>".$depos['size']."</td><td>".$depos['user']."</td><td>".$depos['time_mod']."</td>";
					
					echo "</tr>\n";
				
				}
				echo "</table><br>\n";

			}

	?>

	<br>
	<p align="center"><a href="index.php">Back to List of Planets</a></p>
</body>

</html>
